@inherits ComputedStateComponent<User?>
@inject Session Session
@inject IAuth Auth
@inject BlazorModeHelper BlazorModeHelper
@inject ClientAuthHelper ClientAuthHelper

@{
    var user = State.LastNonErrorValue.OrGuest();
    var usedAuthSchemas = user.Identities.Select(kv => kv.Key.Schema).ToHashSet();
    var unusedAuthSchemas = AuthSchemas.Where(p => !usedAuthSchemas.Contains(p.Name)).ToArray();
}

<Dropdown Class="@CssClass">
    <DropdownToggle Color="Color.Success">
        <Blazorise.Icon Name="FontAwesomeIcons.SignInAlt" /> Sign in @Why
    </DropdownToggle>
    <DropdownMenu>
        @foreach (var (name, displayName) in unusedAuthSchemas) {
            <DropdownItem Color="Color.Primary" Clicked="@(_ => ClientAuthHelper.SignIn(name))">
                With @displayName
            </DropdownItem>
        }
    </DropdownMenu>
</Dropdown>

@code {
    private (string Name, string DisplayName)[] AuthSchemas { get; set; } = Array.Empty<(string, string)>();

    [Parameter]
    public string CssClass { get; set; } = "";
    [Parameter]
    public string Why { get; set; } = "";

    protected override Task<User?> ComputeState(CancellationToken cancellationToken)
        => Auth.GetUser(Session, cancellationToken);

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender) {
            // GetSchemas requires JS interop, so it can be called only at this point
            AuthSchemas = await ClientAuthHelper.GetSchemas();
            StateHasChanged();
        }
    }
}
